package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.Userrole;
import cn.bx.bid.service.RoleService;
import cn.bx.bid.service.UserService;
import cn.bx.bid.util.AjaxResult;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

//登录
    @PostMapping(value = "ulogin",produces = "application/json;charset=utf-8")
    @ResponseBody//映射json
    public AjaxResult userlogin(HttpServletRequest request, @RequestParam("username")String user, @RequestParam("password") String p){
        User u=this.userService.ulogin(user,p);
        if (u != null){
            request.getSession().setAttribute("USER_LOGIN", u);
            return AjaxResult.success("msg",u);
        }
        return AjaxResult.error();
    }
    //管理员分页
    @RequestMapping(value = "usersearch", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> usersearch(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "start", required = false) Date start, @RequestParam(name = "end", required = false) Date end) {
        pageNo--;
        org.springframework.data.domain.Page<User> page = userService.paging(pageNo, limit, name, start, end);
        return Page.toPage(page);
    }

    //添加用户
    @RequestMapping(value = "userAdd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userAdd(@ModelAttribute("u") User user) {
        int r = userService.add(user);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }
    //批量删除
    @RequestMapping(value = "batchDel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids[]") long[] ids) {
        int r = userService.batchDel(ids);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }
    //验证用户名是否重复
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult checkUserNameRepeat(@RequestParam(name="userName") String name, @RequestParam(name="id",required = false) Long id) {

        boolean repeat = this.userService.checkUserNameRepeat(id, name);
        if (repeat){
            return AjaxResult.error("重复");
        }
        return AjaxResult.success("成功");
    }
    //获取值
    @RequestMapping(value = "userGet",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userGet(@RequestParam(name="id")long id) {
        User user = this.userService.get(id);
        return AjaxResult.success("msg",user);
    }
    //修改的下拉框
    @RequestMapping(value = "findRoles",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Userrole> findRoles()
    {
        List<Userrole> li = roleService.finAll();
        // String str = JSON.toJSONStringWithDateFormat(li, "yyyy-MM-dd");
        return li;
    }
    //修改
    @RequestMapping(value = "userUpdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userUpdate(@ModelAttribute("u") User user)
    {
        int r=this.userService.update(user);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error();
    }
    //删除用户
    @RequestMapping(value = "delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult delete(@RequestParam(name = "id",required = false) long id){
        int i=this.userService.del(id);
        if (i>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error();
    }
    //修改密码
    @RequestMapping(value = "UpdatePass")
    public AjaxResult updatePassword(@PathVariable long id,@PathVariable String password){
        int r=userService.resetPass(id, password);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }

    //处理日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
/*@RequestMapping(value = "getUserById/{id}")
public User getUserById(@PathVariable Long id){
    User user=userService.getUserById(id);
    return user;
}

@RequestMapping(value = "delById/{id}")
public String delById(@PathVariable Long id){
    userService.delById(id);
    return "删除了id为"+id+"的人，删除成功";
}

    @RequestMapping(value = "seachName/{name}")
    public List<User> seach(@PathVariable String name){
       return userService.searchName(name);

    }*/

/*    //要用Postman
@RequestMapping(value = "add",produces = "application/json;charset=utf-8")
@ResponseBody
public String add(@ModelAttribute("u") User user){
    userService.add(user);
    return "增加成功";

}*/

}
